import pandas as pd
import numpy as np
from tkinter.filedialog import *
import matplotlib.pyplot as plt
from scipy import stats


def readcsv():
    # read csv by pandas
    filename = askopenfile()
    csv_data = pd.read_csv(filename, header=None)  # no headers
    # read columns to list
    colname = csv_data.columns
    coldata = {}

    for i in colname:
        coldata[i] = csv_data[i].values
        print(coldata[i].dtype)
    return coldata
    # type of coldata[i] is numpy.array
    # type of colname is dict


class signal:
    def __init__(self, sdata, freq):
        self.sdata = sdata
        self.freq = freq

    def mapsignal(self):
        totalfig = len(self.sdata)
        f, axarr = plt.subplots(totalfig, sharex=True, sharey=False)
        axarr[0].set_title('ALL singal')
        for i in range(0, totalfig):
            self.sdata[i] = self.sdata[i].astype(np.float128)  # !!! here only float128 !!! not float, not float64 ...
            print(self.sdata[i].dtype)
            axarr[i].plot(np.arange(0, len(self.sdata[i])/self.freq, 1/self.freq), self.sdata[i], color='r')
            axarr[i].set_xlabel('time(s)')
            axarr[i].set_ylabel('voltage(V)')

    def parameters(self):
        totalfig = len(self.sdata)
        data_mean = []
        data_sigma = []
        data_skew = []
        data_kurtosis = []
        for i in range(0, totalfig):
            sdata_float64 = self.sdata[i].astype(np.float64)
            # ! should run mapsignal fisrt to convert the str to float128 !
            # ! status deal with float64 ! errors with float128
            data_mean.append(np.mean(sdata_float64))
            data_sigma.append(np.std(sdata_float64))
            data_skew.append(stats.skew(sdata_float64))
            data_kurtosis.append(stats.kurtosis(sdata_float64))
        return data_mean, data_sigma, data_skew, data_kurtosis



SamplingF = 2*10**9  # sampling frequency
S = signal(readcsv(), SamplingF)  # select file and read csv

print(type(S))
S.mapsignal()
plt.show()
plt.close()

S_mean, S_sigma, S_skew, S_kurtosis = S.parameters()
print(S_mean)
print(S_kurtosis)
print(S_sigma)
print(S_kurtosis)
